package com.song.pj.ajax.controller;

import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class AjaxApplication {
    private List<Map<String,Object>> dbList=new ArrayList<>();
    public AjaxApplication(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",100);
        map.put("name","锤子");
        map.put("remark","一锤定音");
        map.put("createdTime",new Date());
        dbList.add(map);
    }
    @GetMapping("/doAjaxGet")
    public List<Map<String , Object>> doAjaxGet(){
        return dbList;
    }
    @PostMapping("/doAjaxPost")
    public String doAjaxPost(@RequestParam Map<String,Object> map){
        map.put("createdTime",new Date());
        dbList.add(map);
        return "save ok";
    }
    @DeleteMapping("/doAjaxDelete")
    public String doAjaxDelete( String id){
//        for (Map<String, Object> map:
//        dbList){
//            if(map.get("id").equals(id)) {
//                dbList.remove(map);
//            }
//        }

        Iterator it = dbList.iterator();
        while(it.hasNext()){
            Map<String,Object> map = (Map<String,Object>)it.next();
            if(map.get("id").equals(id)){
                it.remove();
            }
        }
        return "delete ok";
    }
    @PutMapping("/doAjaxUpdate")
    public String doAjaxUpdate(@RequestParam Map<String,Object> Umap){
        Iterator it = dbList.iterator();
        while(it.hasNext()){
            Map<String,Object> map = (Map<String,Object>)it.next();
            if(map.get("id").equals(Umap.get("id"))){
                map.put("name",Umap.get("name"));
                map.put("remark",Umap.get("remark"));
            }

        }
        return "update ok";
    }
    @CrossOrigin
    @RequestMapping("/doAjaxGet01")
    public String doAjaxGet01() throws InterruptedException {
        Thread.sleep(5000);
        return "Response Result Of Ajax Get Requst 01";
    }
}
